package com.dongdongshop.servcice.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.dongdongshop.mapper.TbSpecificationMapper;
import com.dongdongshop.mapper.TbSpecificationOptionMapper;
import com.dongdongshop.pojo.TbSpecification;
import com.dongdongshop.pojo.TbSpecificationExample;
import com.dongdongshop.pojo.TbSpecificationOption;
import com.dongdongshop.pojo.TbSpecificationOptionExample;
import com.dongdongshop.pojo.vo.SpecVo;
import com.dongdongshop.service.SpecificationService;
import com.dongdongshop.util.PageBean;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.stream.Stream;

@Service
@Component
public class SpecificationServiceImpl implements SpecificationService {
    @Resource
    private TbSpecificationMapper specificationMapper;
    @Resource
    private TbSpecificationOptionMapper specificationOptionMapper;

    @Override
    public PageBean<TbSpecification> getSpecification(String specName,Integer pageNumber,Integer pageSize) {

        PageBean<TbSpecification> page = new PageBean<>();
        page.setPageNumber(pageNumber);
        page.setPageSize(pageSize);
        List<TbSpecification> list = specificationMapper.getSpecificationByName(specName,page.getStartIndex(),pageSize);
        List<TbSpecification> tbSpecifications = specificationMapper.selectByExample(new TbSpecificationExample());
        page.setTotalCount(tbSpecifications.size());
        page.setResult(list);
        return page;
    }

    @Transactional //声明事务
    @Override
    public void insertSpec(String specName, Integer[] orders, String[] optionName) {
        //增加并返回id
        TbSpecification specification = new TbSpecification();
        specification.setSpecName(specName);
        specificationMapper.insertSpecAndBackId(specification);
        Long id = specification.getId();

        List<TbSpecificationOption> specificationOptions = new ArrayList<>();
        for(int i = 0; i <orders.length ; i++){
            TbSpecificationOption specificationOption = new TbSpecificationOption();
            specificationOption.setOrders(orders[i]);
            specificationOption.setSpecId(specification.getId());
            specificationOption.setOptionName(optionName[i]);
            specificationOptions.add(specificationOption);
        }

        /*TbSpecificationOption specificationOption = new TbSpecificationOption();
        Stream<Integer> orders1 = Stream.of(orders);
        Consumer<Integer> consumer = x ->{
            specificationOption.setOrders(x);
            specificationOption.setSpecId(specification.getId());
        };
        orders1.forEach(consumer);

        Stream<String> optionName1 = Stream.of(optionName);
        Consumer<String> consumer1 = x ->{
            specificationOption.setOptionName(x);
            specificationOptions.add(specificationOption);
        };
        optionName1.forEach(consumer1);*/

        //批量增加
        specificationOptionMapper.insertBath(specificationOptions);

    }

    @Transactional //声明事务
    @Override
    public void deleteBatch(Integer[] ids) {
        specificationMapper.deleteByPrimaryKey(Long.valueOf(ids[0]));

        TbSpecificationOptionExample specificationOptionExample = new TbSpecificationOptionExample();
        TbSpecificationOptionExample.Criteria criteria = specificationOptionExample.createCriteria();
        criteria.andSpecIdEqualTo(Long.valueOf(ids[0]));
        specificationOptionMapper.deleteByExample(specificationOptionExample);
    }
    //回显
    @Override
    public List<SpecVo> getSpecVoById(Integer id) {
        List<SpecVo> list = specificationMapper.getSpecVoById(id);
        return list;
    }

    @Transactional //声明事务
    @Override
    public void updateSpec(TbSpecification specification, Integer[] orders, String[] optionName) {
        specificationMapper.updateByPrimaryKeySelective(specification);

        TbSpecificationOptionExample example = new TbSpecificationOptionExample();
        TbSpecificationOptionExample.Criteria criteria = example.createCriteria();
        criteria.andSpecIdEqualTo(specification.getId());
        specificationOptionMapper.deleteByExample(example);

        List<TbSpecificationOption> specificationOptions = new ArrayList<>();
        for(int i = 0; i <orders.length ; i++){
            TbSpecificationOption specificationOption = new TbSpecificationOption();
            specificationOption.setOrders(orders[i]);
            specificationOption.setSpecId(specification.getId());
            specificationOption.setOptionName(optionName[i]);
            specificationOptions.add(specificationOption);
        }
        //批量增加
        specificationOptionMapper.insertBath(specificationOptions);
    }

    @Override
    public List<TbSpecification> getSpecAllInfo() {
        List<TbSpecification> specifications = specificationMapper.selectByExample(new TbSpecificationExample());
        return specifications;
    }

    @Override
    public Map<Integer, List<TbSpecificationOption>> getSpecById(Integer[] specId) {
        Map<Integer, List<TbSpecificationOption>> map = new HashMap<>();

        Stream<Integer> specId1 = Stream.of(specId);
        Consumer<Integer> consumer = x ->{
            TbSpecificationOptionExample example = new TbSpecificationOptionExample();
            TbSpecificationOptionExample.Criteria criteria = example.createCriteria();
            criteria.andSpecIdEqualTo(Long.valueOf(x));
            List<TbSpecificationOption> specificationOptions = specificationOptionMapper.selectByExample(example);
            map.put(x,specificationOptions);
        };
        specId1.forEach(consumer);

        /*for (int i= 0;i<specId.length;i++){
            TbSpecificationOptionExample example = new TbSpecificationOptionExample();
            TbSpecificationOptionExample.Criteria criteria = example.createCriteria();
            criteria.andSpecIdEqualTo(Long.valueOf(specId[i]));
            List<TbSpecificationOption> specificationOptions = specificationOptionMapper.selectByExample(example);
            map.put(specId[i],specificationOptions);
        }*/
        return map;
    }
}
